package com.leyou.user.controller;

import com.leyou.common.exception.pojo.LyException;
import com.leyou.user.entity.HisImgs;
import com.leyou.user.entity.User;
import com.leyou.user.entity.UserInfo;
import com.leyou.user.service.HisImgsService;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private HisImgsService hisImgsService;
    /**
     * 校验用户名或手机号是否唯一
     */
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUsernameOrPhone(@PathVariable("data") String data,
                                                        @PathVariable("type") Integer type){
        Boolean isTrue = userService.checkUsernameOrPhone(data, type);
        return ResponseEntity.ok(isTrue);
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendCheckCode(@RequestParam("phone") String phone){
        userService.sendCheckCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 用户注册
     * BindingResult 这个对象要紧挨着被验证的对象放，可以起到收集数据格式异常的作用
     */
    @PostMapping("/register")
    public ResponseEntity<Void> register(@Valid User user, BindingResult result,
                                         @RequestParam("code") String code){
        //判断是否有数据格式异常
        if(result.hasErrors()){
            String errorMsg = result.getFieldErrors().stream()
                    .map(FieldError::getDefaultMessage)//只收集出错的字段的defaultMessage
                    .collect(Collectors.joining("|"));
            //抛出自定义异常
            throw new LyException(501, errorMsg);
        }
        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据用户名和密码查询用户
     */
    @GetMapping("/query")
    public ResponseEntity<User> findUserByUsernameAndPassword(@RequestParam("username") String username,
                                                              @RequestParam("password") String password){
        User user = userService.findUserByUsernameAndPassword(username, password);
        return ResponseEntity.ok(user);
    }
    @GetMapping("/loadUserInfo")
    public ResponseEntity<UserInfo>getUserInfo(@RequestParam("userId")Long userId){
       UserInfo userInfo=userService.getUserInfo(userId);

        return ResponseEntity.ok(userInfo);
    }
    @GetMapping("/loadHisImages")
    public ResponseEntity<List<HisImgs>>loadHisImages(@RequestParam("userId")Long userId){

            List<HisImgs> hisImgsList=hisImgsService.loadHisImages(userId);
        return ResponseEntity.ok(hisImgsList);
    }
    @PostMapping("/saveUserInfo")
    public ResponseEntity<UserInfo>getUserInfo(UserInfo userInfo,@RequestParam(value = "baseData",required = false)String baseData)throws Exception{
        userService.saveUserInfo(userInfo,baseData);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
